package es.pawer.android.listacompras.biz;

import java.util.ArrayList;
import java.util.List;

import es.pawer.android.listacompras.data.ShoppingListTO;

/**
 * Adds needed behaviour to a ShoppingTO. The list of items is now an attribute of the Shopping list.
 * @author plo
 *
 */
public class ShoppingList extends ShoppingListTO {
	
	private static final long serialVersionUID = 7584388868048130905L;
	
	private List<Item> itemList;
	int numberOfSelectedItems=0;

	public ShoppingList(String s) {
		super(s);
		itemList=null;
	}

	public ShoppingList() {
		super();
	}

	public ShoppingList(ShoppingListTO to) {
		super(to.getName());
		this.setCreated(to.getCreated());
		this.setId(to.getId());
		this.itemList=null;
	}
	
	public void select (int i) {
		this.itemList.get(i).addOrderPoints(this.itemList.size()-numberOfSelectedItems);
		numberOfSelectedItems++;
		this.itemList.get(i).setSelected(true);
	}
	
	public void deselect(int i) {
		this.itemList.get(i).setSelected(false);
		numberOfSelectedItems--;
	}
	
	
	public boolean isEveryItemSelected() {
		if (this.itemList.size()>0) {
			for (Item g: this.itemList) {
				if (!g.isSelected()) {
					return false;
				}
			}
		}
		return true;
	}

	
	public void normalizePoints() {
		int lowest=999;
		for (Item g: this.itemList) {
			if (g.getOrderPoints()<lowest) {
				lowest=g.getOrderPoints();
			}
		}
		for (Item g: this.itemList) {
			g.setOrderPoints(g.getOrderPoints()-lowest+1);
		}
	}
	
	public List<String> getNames() {
		List<String> names=new ArrayList<String>();
		for (Item g: this.itemList) {
			names.add(g.getName());
		}
		
		return names;
	}

	public List<Item> getItemList() {
		return itemList;
	}

	public void setItemList(List<Item> itemList) {
		this.itemList = itemList;
	}

	
}
